﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Frame.Lib.Core.Role;
using Frame.Lib.Utilities;
using Frame.Lib.Web;
using Frame.Lib.Common;


public partial class Admin_Role_List : PageList
{
    private RoleBO Role
    {
        get
        {
            return RoleBO.Instance<RoleBO>();
        }
    }
    //private PagerSortSetting _setting;
    protected void Page_Load(object sender, EventArgs e)
    {
       // _setting = Footer1.InitPager(new BindViewHandler(BindView));
        if (!IsPostBack)
        {
            BindView();
        }
        AddAttr();
        base.InitSelectID(hidSelectIDs);
        JSUtil.SetTableHeight("list", 160);       
    }

    private void AddAttr()
    {     
        JSUtil.DelConfirm(btnDelete, "角色");
        JSUtil.ShowForm(lnkAdd,"增加角色", null, GlobalVar.WebCurrPath + "Edit.aspx", 380, 176);
        JSUtil.ClickBtn(lnkDelete, btnDelete);
    }

    private void BindView()
    {
        LVUtil.Init(lv, lvHeader);
        chkSelectAll.Checked = false;

        PagerSetting setting = new PagerSetting();
        setting.PageIndex = this.PageIndex;
        setting.PageSize = AppSetting.PageSize;
        setting.OrderBy = "TypeID";

        IEnumerable<RoleInfo> list = Role.GetList().Skip((setting.PageIndex - 1) * setting.PageSize).Take(setting.PageSize);
        setting.RecordCount = list.Count();
        lv.DataSource = list;
        lv.DataKeyNames = new string[] { "ID" };
        lv.DataBind();

        string prefix = GlobalVar.WebCurrPath + "List.aspx";
        litPager.Text = HtmlPager.Render(setting, prefix, true);

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }
        List<int> ids = StrUtil.SplitToInt(strSelectIDs);
        Role.Delete(ids);
        BindView();
    }
    
    protected bool IsEdit(object typeID)
    {
        RoleType roleType = (RoleType)(int)typeID;
        if (roleType == RoleType.内置)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            LVUtil.SetEdit(sender, e, 380, 174);
            string strRoleID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
            string strDlgUrl = "SaveModule.aspx?RoleID=" + strRoleID + "&flag=1";
            Image imgModule = (Image)e.Item.FindControl("imgModule");
            imgModule.Style.Add("cursor", "pointer");
            imgModule.Attributes.Add("onclick", "showDlg('" + strDlgUrl + "')");

            strDlgUrl = "SaveUser.aspx?RoleID=" + strRoleID;
            Image imgUser = (Image)e.Item.FindControl("imgUser");
            imgUser.Style.Add("cursor", "pointer");
            imgUser.Attributes.Add("onclick", "showDlg('" + strDlgUrl + "');");

            strDlgUrl = "SaveRight.aspx?RoleID=" + strRoleID;
            Image imgRight = (Image)e.Item.FindControl("imgRight");
            imgRight.Style.Add("cursor", "pointer");
            imgRight.Attributes.Add("onclick", "showCustDlg('" + strDlgUrl + "',null,350,null);");
        }
    }
}
